<template>
  <div class="report-form-content">
    <el-tabs type="border-card">
      <el-tab-pane label="合同验收">
        <div class="report-list">
          <label>验收内容：</label>
          <span>{{ obj.content }}</span>
        </div>
        <div class="report-list">
          <label>验收负责人：</label>
          <span>{{ showIdLabel(userList, obj.principalUserId, 'name') }}</span>
        </div>
        <div class="report-list">
          <label>监理：</label>
          <span>{{ showIdLabel(userList, obj.supervisorUserId, 'name') }}</span>
        </div>
        <div class="report-list">
          <label>验收时间：</label>
          <span>{{ obj.checkingTime }}</span>
        </div>
        <div class="report-list">
          <label>验收意见：</label>
          <span>{{ obj.checkingComment }}</span>
        </div>
        <div class="report-list">
          <label>审批方式：</label>
          <span>{{ obj.approvalMethod === '1' ? '线上审批' : '线下审批' }}</span>
        </div>
        <div class="report-list">
          <label>验收状态：</label>
          <span><EnumTag :enum-list="getDictList('ContractInitialCheckingState')" :enum-key="obj.state" /></span>
        </div>
      </el-tab-pane>
      <el-tab-pane label="验收附件">
        <el-card class="update-log">
          <!-- 附件信息 -->
          <el-table :data="oFile"
            style="width: 100%; margin-top: 20px"
            row-key="id"
            border
            default-expand-all>
            <el-table-column align="center" label="验收附件" min-width="250px">
              <template v-slot="scope">
                <ul class="files-url">
                  <li class="files-green" v-for="item1 in scope.row.attachments" :key="item1.id" v-show="item1.attachmentType === '01'">
                    <div class="file-text" v-if="item1.attachmentType === '01'">
                      <div>
                        <span class="file-name">{{ item1.attachmentName }}</span>
                        <span class="file-kb">{{ bytesToSize(item1.attachmentSize) }}</span>
                      </div>
                      <div>
                        <span class="file-time">{{ showIdLabel(userList, item1.attachmentUploader, 'name') }}<span style="color:#333;">上传于</span>{{ item1.attachmentUpload }}</span>
                      </div>
                    </div>
                    <div class="file-btn">
                      <el-button type="primary" link @click="downloadFile(item1)">下载</el-button>
                      <el-button type="primary" link @click="handlePreview(item1)">预览</el-button>
                    </div>
                  </li>
                </ul>
              </template>
            </el-table-column>
            <el-table-column align="center" label="签名/盖章附件" min-width="250px">
              <template v-slot="scope">
                <ul class="files-url">
                  <li class="files-green" v-for="item1 in scope.row.attachments" :key="item1.id" v-show="item1.attachmentType === '02'">
                    <div class="file-text">
                      <div>
                        <span class="file-name">{{ item1.attachmentName }}</span>
                        <span class="file-kb">{{ bytesToSize(item1.attachmentSize) }}</span>
                      </div>
                      <div>
                        <span class="file-time">{{ showIdLabel(userList, item1.attachmentUploader, 'name') }}<span style="color:#333;">上传于</span>{{ item1.attachmentUpload }}</span>
                      </div>
                    </div>
                    <div class="file-btn">
                      <el-button type="primary" link @click="downloadFile(item1)">下载</el-button>
                      <el-button type="primary" link @click="handlePreview(item1)">预览</el-button>
                    </div>
                  </li>
                </ul>
              </template>
            </el-table-column>
            <el-table-column align="center" label="验收结果附件" min-width="250px">
              <template v-slot="scope">
                <ul class="files-url">
                  <li class="files-green" v-for="item1 in scope.row.attachments" :key="item1.id" v-show="item1.attachmentType === '03'">
                    <div class="file-text">
                      <div>
                        <span class="file-name">{{ item1.attachmentName }}</span>
                        <span class="file-kb">{{ bytesToSize(item1.attachmentSize) }}</span>
                      </div>
                      <div>
                        <span class="file-time">{{ showIdLabel(userList, item1.attachmentUploader, 'name') }}<span style="color:#333;">上传于</span>{{ item1.attachmentUpload }}</span>
                      </div>
                    </div>
                    <div class="file-btn">
                      <el-button type="primary" link @click="downloadFile(item1)">下载</el-button>
                      <el-button type="primary" link @click="handlePreview(item1)">预览</el-button>
                    </div>
                  </li>
                </ul>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>
      <el-tab-pane label="合同详情">
        <contractMainDetail :contractInitialId="obj.contractInitialId" />
      </el-tab-pane>
    </el-tabs>
    <el-dialog v-model="attachmentVisible" title="附件预览" width="70%" top="2vh">
      <attachmentPreview :attachmentId="previewAttachmentId" />
      <div style="text-align:right;">
        <el-button type="danger" @click="attachmentVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import {ref, watch} from 'vue'
import {doGet, doGetDownload} from "@/config/http";
import {svc, showIdLabel, getListPageSize} from "@/config/common";
import contractMainDetail from '@/h5/components/contractMainDetail.vue'
import attachmentPreview from '@/views/components/base/attachmentPreview.vue'
import initListStore from '@/store/modules/initList'

const props = defineProps({
  contractCheckingId: {
    required: true,
    type: String,
  }
})

let previewAttachmentId = ref('')
let attachmentVisible = ref(false)
let obj = ref(null)
let oFile = ref([])
let objList = ref([])
let isfile1 = ref(false)
let isfile2 = ref(false)
let isfile3 = ref(false)

let userList = initListStore().userList // 用户

// const initList = async () => {
//   let p1 = doGet(svc, '/user/page', {pageSize: getListPageSize()}, false) // 用户
//   let resArray = await Promise.all([p1])
//   userList.value = resArray[5]['records']
// }

const initObj = async (val) =>  {
  if (val) {
    obj.value = await doGet(svc, `/contract-initial-checking/${val}`, {}, true)
    oFile.value = await doGet(svc, '/contract-initial-checking-attachment/listByField', {field: 'contractInitialCheckingId', values: obj.value.id}, false)
    isfile1.value = oFile.value.every(item => item.attachmentType === '01')
    isfile2.value = oFile.value.every(item => item.attachmentType === '02')
    isfile3.value = oFile.value.every(item => item.attachmentType === '03')
  }
}

watch(() => props.contractCheckingId, async val => {
  await initObj(val)
  // await initList()
},{immediate:true})

const downloadFile = (row) => {
  doGetDownload(svc, '/customAttachment/download/' + row.attachmentId, {})
}
const handlePreview = (file) => {
  attachmentVisible.value = true
  setTimeout(() => {
    previewAttachmentId.value = file.attachmentId
  },100)
}
</script>

<style scoped lang="scss">
.report-form-content {
  width: 100%;
  height: 52vh;
  overflow-x: hidden;
  overflow-y: auto;
  margin-top: 1.25rem;
}
.update-log {
  width: 100%;
  margin: 30px 0;
}
.tabs-form-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tabs-form-head span {
  font-size: 18px;
  color: #303133;
  font-weight: bold;
}
.report-list {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--van-cell-background);
  box-sizing: border-box;
  width: 100%;
  padding: var(--van-cell-vertical-padding) var(--van-cell-horizontal-padding);
  overflow: hidden;
  color: var(--van-cell-text-color);
  font-size: var(--van-cell-font-size);
  // line-height: var(--van-cell-line-height);
  border-bottom: 1px solid var(--van-border-color);;
  & label {
    color: var(--van-field-label-color);
    // text-align: left;
    word-wrap: break-word;
    font-weight: 700;
  }
  & span {
    display: block;
    box-sizing: border-box;
    // width: 100%;
    color: var(--van-field-input-text-color);
    line-height: inherit;
    // text-align: left;
    background-color: transparent;
    border: 0;
    resize: none;
    -webkit-user-select: auto;
    user-select: auto;
  }
}
</style>
